﻿Imports System.Math

Public Class Point3d

    Public x As Double
    Public y As Double
    Public z As Double
    Public c As Color

    Sub New(ByVal x As Double, ByVal y As Double, ByVal z As Double)
        Me.New(x, y, z, Color.Black)
    End Sub

    Sub New(ByVal x As Double, ByVal y As Double, ByVal z As Double, ByVal c As Color)
        Me.x = x
        Me.y = y
        Me.z = z
        Me.c = c
    End Sub

    ReadOnly Property y2d(ByVal ed As Double, ByVal cd As Double) As Double
        Get
            Return ed * y / (ed + cd + z)
        End Get
    End Property

    ReadOnly Property x2d(ByVal ed As Double, ByVal cd As Double) As Double
        Get
            Return ed * x / (ed + cd + z)
        End Get
    End Property

    ReadOnly Property p(ByVal ed As Double, ByVal cd As Double) As Point
        Get
            Return New Point(x2d(ed, cd), -y2d(ed, cd))
        End Get
    End Property

    Sub Rotate(ByVal a1 As Double, ByVal a2 As Double)
        RotateX(a1)
        RotateY(a2)
    End Sub

    Sub RotateX(ByVal a As Double)
        Dim o = New Point3d(x, y, z)
        y = o.y * Cos(a) - o.z * Sin(a)
        z = o.y * Sin(a) + o.z * Cos(a)
    End Sub

    Sub RotateY(ByVal a As Double)
        Dim o = New Point3d(x, y, z)
        x = o.x * Cos(a) - o.z * Sin(a)
        z = o.x * Sin(a) + o.z * Cos(a)
    End Sub

    Sub RotateZ(ByVal a As Double)
        Dim o = New Point3d(x, y, z)
        x = o.x * Cos(a) - o.y * Sin(a)
        y = o.x * Sin(a) + o.y * Cos(a)
    End Sub

    Sub Offset(ByVal x As Double, ByVal y As Double, ByVal z As Double)
        Me.x += x
        Me.y += y
        Me.z += z
    End Sub

    ReadOnly Property Distance(ByVal p As Point3d) As Double
        Get
            Return z
            'Return Sqrt((p.x - x) ^ 2 + (p.y - y) ^ 2 + (p.z - z) ^ 2)
        End Get
    End Property

End Class